package cn.tx.controller;

import cn.tx.model.Student;
import cn.tx.model.User;
import cn.tx.service.StudentService;
import cn.tx.service.UserService;
import cn.tx.util.CpachaUtil;
import cn.tx.util.GetRequestJsonUtils;
import com.alibaba.fastjson.JSONObject;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@CrossOrigin
@RequestMapping(path = "/app")
public class AppController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;



    /*输入账号后的登录跳转*/
   @RequestMapping(path = "/login", method = RequestMethod.POST)
   @ResponseBody
    public Map<String,String> login1(
           HttpServletRequest request
   ) throws IOException {
       JSONObject json = GetRequestJsonUtils.getRequestJsonObject(request);
       String username = json.getString("username");
       String password = json.getString("password");

       Map<String,String> map = new HashMap<>();
       if (StringUtils.isEmpty(username)){
           map.put("type","error");
           map.put("msg","用户名不能为空");
           return map;
       }
       if (StringUtils.isEmpty(password)){
           map.put("type","error");
           map.put("msg","密码不能为空");
           return map;
       }
      int type=1;
       //数据库查询用户名和密码
       if(type==1){
           //管理员
           User name = userService.findByUserName(username);
           if (name==null){
               map.put("type","error");
               map.put("msg","用户不存在，请先注册");
               return map;
           }
           if (!password.equals(name.getPassword())){
               map.put("type","error");
               map.put("msg","密码错误请重试");
               return map;
           }
           map.put("username",name.getUsername());
       }
       if (type==2){
           //学生
           Student student = studentService.findByUserName(username);
           if(student == null){
               map.put("type", "error");
               map.put("msg", "不存在该学生!");
               return map;
           }
           if(!password.equals(student.getPassword())){
               map.put("type", "error");
               map.put("msg", "密码错误!");
               return map;
           }
           map.put("username",student.getUsername());
       }

       map.put("type","success");
       map.put("msg","登录成功!");
       return map;
    }


}
